package com.heima.wemedia.service.impl;
/*
    
 需求描述：
 @Author: wang
 @Date: 2022/10/21 17:09
 
 */


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.dtos.WmSensitivePageReqDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    /**
     * 添加敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitiveDto dto) {
        if (dto != null && dto.getSensitives() != null) {
            WmSensitive sensitive = new WmSensitive();
            sensitive.setSensitives(dto.getSensitives());
            sensitive.setCreatedTime(new Date());
            save(sensitive);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 获取敏感词列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listSensitive(WmSensitivePageReqDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        dto.checkParam();

        LambdaQueryWrapper<WmSensitive> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            lqw.eq(WmSensitive::getSensitives, dto.getName());
        }
        Page<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        page = page(page, lqw);
        List<WmSensitive> sensitiveList = page.getRecords();
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setSize(dto.getSize());
        pageResponseResult.setCurrentPage(dto.getPage());
        pageResponseResult.setTotal((int) page.getTotal());
        pageResponseResult.setData(sensitiveList);

        return pageResponseResult;
    }

    /**
     * 修改敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitiveDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getId()==null||dto.getSensitives()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(dto,wmSensitive);
        boolean flag = updateById(wmSensitive);
        if (flag){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(401,"Unauthorized,修改失败");

    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        boolean flag = removeById(id);

        if (flag){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(401,"删除失败");

    }
}
